cmake_minimum_required(VERSION 3.16)

project(qscreenshot VERSION 0.1 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt6 REQUIRED COMPONENTS Quick Widgets Qml Core5Compat QuickControls2)

qt_standard_project_setup(REQUIRES 6.8)

# 启用自动资源编译
set(CMAKE_AUTORCC ON)

qt_add_executable(appqscreenshot
    main.cpp screenshot.qrc logo.rc
)

qt_add_qml_module(appqscreenshot
    URI qscreenshot
    VERSION 1.0
    QML_FILES
        Main.qml
        SOURCES src/ScreenshotHelper.h
        SOURCES src/ScreenshotHelper.cpp
        SOURCES src/QHotkey/qhotkey.h src/QHotkey/qhotkey_p.h
        SOURCES src/QHotkey/qhotkey.cpp
        SOURCES src/confighelper.h src/confighelper.cpp
        SOURCES src/pixmapprovider.h
)

# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
set_target_properties(appqscreenshot PROPERTIES
#    MACOSX_BUNDLE_GUI_IDENTIFIER com.example.appqscreenshot
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)

target_link_libraries(appqscreenshot
    PRIVATE Qt6::Quick Qt6::Widgets Qt6::Qml Qt6::Core5Compat Qt::QuickControls2
)

if(APPLE)
    find_library(CARBON_LIBRARY Carbon)
    mark_as_advanced(CARBON_LIBRARY)

    target_sources(appqscreenshot PRIVATE src/QHotkey/qhotkey_mac.cpp)
    target_link_libraries(appqscreenshot PRIVATE ${CARBON_LIBRARY})
elseif(WIN32)
    target_sources(appqscreenshot PRIVATE src/QHotkey/qhotkey_win.cpp)
else()
    find_package(X11 REQUIRED)
    if(QT_DEFAULT_MAJOR_VERSION GREATER_EQUAL 6)
        target_link_libraries(appqscreenshot PRIVATE ${X11_LIBRARIES})
    else()
        find_package(Qt${QT_DEFAULT_MAJOR_VERSION} COMPONENTS X11Extras REQUIRED)
        target_link_libraries(appqscreenshot
            PRIVATE
                ${X11_LIBRARIES}
                Qt${QT_DEFAULT_MAJOR_VERSION}::X11Extras)
    endif()

    include_directories(${X11_INCLUDE_DIR})
    target_sources(appqscreenshot PRIVATE src/QHotkey/qhotkey_x11.cpp)
endif()


include(GNUInstallDirs)
install(TARGETS appqscreenshot
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
